﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dos.ORM;
using Model;

namespace Logic
{
    public class SaleInfoLogic
    {
        /// <summary>
        /// 获取所有账户列表
        /// </summary>
        /// <returns></returns>
        public static List<TB_Sale_Info> GetAll()
        {
            return DB.Context.From<TB_Sale_Info>()
                .ToList();
        }

        /// <summary>
        /// 获取需要发送推荐的账户
        /// </summary>
        /// <returns></returns>
        public static TB_Sale_Info GetNeedRecommand()
        {

            //获取没有使用，没有到达限制，可以推荐的 店员账户
            List<TB_Sale_Info> list = DB.Context.From<TB_Sale_Info>()
                .Where(d => d.RecommandState == 0 && d.bUsed == 0 && d.bLimit == 0)
                .ToList();
            if (list.Count > 0)
            {
                return list[new Random().Next(list.Count)];
            }
            else
            {
                //获取没有使用，昨天到达限制，可以推荐的 店员账户
                DateTime today = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")+" 00:00:00");
                list = DB.Context.From<TB_Sale_Info>()
                .Where(d => d.RecommandState == 0 && d.bUsed == 0 && d.bLimit == 1 && d.LimitTime < today)
                .ToList();
                if (list.Count > 0)
                {
                    return list[new Random().Next(list.Count)];
                }
                else
                {
                    return null;
                }
                
            }
        }

        /// <summary>
        /// 获取需要发送推荐的账户
        /// </summary>
        /// <returns></returns>
        public static TB_Sale_Info GetModelByPhone(string phone)
        {
            return DB.Context.From<TB_Sale_Info>()
                .Where(d => d.SaleAccount == phone)
                .ToFirst();
        }

        public static int UpdateModel2NoRecommand(TB_Sale_Info saleInfo)
        {
            saleInfo.RecommandState = 1;
            saleInfo.bUsed = 0;
            return DB.Context.Update<TB_Sale_Info>(saleInfo);
        }

        public static int UpdateModel2CanRecommand(List<string> accountList)
        {
            int Recommand = 0;

            return DB.Context.Update<TB_Sale_Info>(TB_Sale_Info._.RecommandState, Recommand, TB_Sale_Info._.SaleAccount.SelectIn<string>(accountList));
        }

        public static int UpdateModel(TB_Sale_Info saleInfo)
        {
            return DB.Context.Update<TB_Sale_Info>(saleInfo);
        }
    }
}
